"""
传感器 测试页面
"""
import typing
from datetime import datetime

from elio import core

# 测试传感器类型
TEXT_TYPE: typing.Final[int] = core.Sensor.TYPE_ACCELEROMETER
# 采样频率
SAMPLINGPERIOD: typing.Final[int] = core.Sensor.SENSOR_DELAY_UI


class SensorLayer(core.AutoRecycleLayer[core.Activity]):
    sensor: core.Sensor
    tc_list: core.WidgetEditor
    tc_info: core.WidgetEditor
    btn_reg: core.WidgetButton
    btn_un_reg: core.WidgetButton

    async def on_create(self, *args: typing.Any, **kwargs: typing.Dict):
        await super().on_create(*args, **kwargs)
        self.sensor = await core.Sensor.create(self.parent)

    async def on_destroy(self, *args: typing.Any, **kwargs: typing.Dict):
        await super().on_destroy()

    async def on_show(self):
        await super().on_show()
        await self.init_ui()
        await self.show_list()

    async def on_hide(self):
        await super().on_hide()
        await self.un_init_ui()

    async def init_ui(self):
        activity = self.parent
        width, height = await activity.get_screen_size()
        self.tc_list = await core.WidgetEditor.create(activity, width - 60, 200, 30, 30)
        self.tc_info = await core.WidgetEditor.create(activity, width - 60, 200, 30, 260)
        self.btn_reg = await self.create_button("注册监听", 500, self.cb_reg)
        self.btn_un_reg = await self.create_button("取消监听", 550, self.cb_un_reg)

    async def un_init_ui(self):
        await self.tc_list.destroy()
        await self.tc_info.destroy()
        await self.btn_reg.destroy()
        await self.btn_un_reg.destroy()

    async def on_pause(self):
        await super().on_pause()

    async def on_resume(self):
        await super().on_resume()

    async def create_button(self, name: str, pos_y: int, cb: typing.Callable) -> core.WidgetButton:
        """
        创建按钮。
        :param name: 按钮文字。
        :param pos_y: 纵坐标。
        :param cb: 点击处理函数。
        :return: core.WidgetButton
        """
        activity = self.parent
        width, _ = await activity.get_screen_size()

        button = await core.WidgetButton.create(activity, width - 60, 50, 30, pos_y, name)
        await button.set_on_click_listener(cb)
        return button

    async def show_list(self):
        sensors = await self.sensor.get_list()
        text = '\n'.join([str(sens) for sens in sensors])
        await self.tc_list.set_text(text)

    async def cb_reg(self):
        await self.tc_info.set_text('正在注册监听...')
        await self.sensor.register_listener(TEXT_TYPE,
                                            self.sensor_callback, self.accuracy_callback,
                                            SAMPLINGPERIOD)

    async def cb_un_reg(self):
        await self.tc_info.set_text('已取消注册监听')
        await self.sensor.unregister_listener()

    async def sensor_callback(self, num_1: float, num_2: float, num_3: float):
        timer = datetime.now().strftime("%H:%M:%S")
        text = f'{timer}\n传感器当前值: {num_1}, {num_2}, {num_3}'
        await self.tc_info.set_text(text)

    async def accuracy_callback(self, num: int):
        timer = datetime.now().strftime("%H:%M:%S")
        text = f'{timer}\n传感器当前精度值: {num}'
        await self.tc_info.set_text(text)
